import { LIST, LIST_BUTTON, FIELD, LIST_DISABLED_BUTTON, STATUS } from "../../constant";

//选择事件示例

//单选 props, moduleId(区域id), record（行数据）, index（当前index）, status（选框值）
export function selectedEvent(props, moduleId, record, index, status) {
    props.button.setButtonDisabled(LIST_DISABLED_BUTTON, true);

    let showBtn = [], deleteFlag = true, isAllNoState = true, isAllPassing = true, bluePzFlag = true, redPzFlag = true, noticeFlag = true, noticeQRFlag = true;
    let selectDatas = props.table.getCheckedRows(LIST.table_id);

    if (selectDatas.length == 0) {
        return;
    } else if (selectDatas.length > 1) { //多选
        showBtn = [LIST_BUTTON.print, LIST_BUTTON.output];
        for (let selectData of selectDatas) {
            let status = selectData.data.values[FIELD.billStatus].value;
            if (status == STATUS.PASSING || status == STATUS.COMMIT || status == STATUS.GOINGON) {
                isAllNoState = false;
                deleteFlag = false;

                if(status == STATUS.PASSING){

                    let ishasBluePz = false;
                    let pk_detail = selectData.data.values['pzid'] && selectData.data.values['pzid'].value;
                    if(pk_detail != null){
                            ishasBluePz = true;
                    }
                    let ishasRedPz = false;
                    let pk_detail_h = selectData.data.values['hcid'] && selectData.data.values['hcid'].value;
                    if(pk_detail_h != null){
                            ishasRedPz = true;
                    }
                    let ishasQR = false;
                    let isflag = selectData.data.values['isflag'] && selectData.data.values['isflag'].value;
                    if(isflag != null){
                            ishasQR = isflag;
                    }

                    if(ishasBluePz){
                        bluePzFlag = false;
                        if(ishasRedPz){
                            redPzFlag = false;
                        }else{
                                if(ishasQR){
                                    noticeFlag = false;
                                    noticeQRFlag = false;
                                }
                        }
                    }else{
                        redPzFlag = false;
                        noticeFlag = false;
                        noticeQRFlag = false;
                    }
                }else{
                    bluePzFlag = false;
                    redPzFlag = false;
                    noticeFlag = false;
                    noticeQRFlag = false;
                }
            } else if (status == STATUS.NOSTATE) {
                isAllPassing = false;
            }
        }
        if (deleteFlag) {
            showBtn.push(LIST_BUTTON.delete);
        }
        if (isAllNoState) {
            showBtn.push(LIST_BUTTON.commit);
        }
        if (isAllPassing) {
            showBtn.push(LIST_BUTTON.unCommit);
        }
        if (bluePzFlag) {
            showBtn.push(LIST_BUTTON.bluePzlistBtn);
        }
        if (redPzFlag) {
            showBtn.push(LIST_BUTTON.redPzlistBtn);
        }
        if (noticeFlag) {
            showBtn.push(LIST_BUTTON.noticelistBtn);
        }
        if (noticeQRFlag) {
            showBtn.push(LIST_BUTTON.noticelistQrBtn);
        }
        props.button.setButtonDisabled(showBtn, false);
    } else { //单选
        let busistatus = selectDatas[0].data.values[FIELD.billStatus] && selectDatas[0].data.values[FIELD.billStatus].value;
        if (busistatus == STATUS.NOSTATE) { //自由
            showBtn = [LIST_BUTTON.commit, LIST_BUTTON.delete];
        } else if (busistatus == STATUS.PASSING || busistatus == STATUS.COMMIT || busistatus == STATUS.GOINGON) { //提交
            if(busistatus == STATUS.PASSING){
                showBtn = [LIST_BUTTON.cancelApprover];

                let ishasBluePz = false;
                let pk_detail = selectDatas[0].data.values['pzid'] && selectDatas[0].data.values['pzid'].value;
                if(pk_detail != null){
                        ishasBluePz = true;
                }
                let ishasRedPz = false;
                let pk_detail_h = selectDatas[0].data.values['hcid'] && selectDatas[0].data.values['hcid'].value;
                if(pk_detail_h != null){
                        ishasRedPz = true;
                }
                let ishasQR = false;
                let isflag = selectDatas[0].data.values['isflag'] && selectDatas[0].data.values['isflag'].value;
                if(isflag != null){
                        ishasQR = isflag;
                }

                if(ishasBluePz){
                        if(ishasRedPz){
                        }else{
                                if(ishasQR){
                                    showBtn.push(LIST_BUTTON.redPzlistBtn);
                                }else{
                                    showBtn.push(LIST_BUTTON.noticelistBtn, LIST_BUTTON.noticelistQrBtn);
                                }
                        }
                }else{
                    showBtn.push(LIST_BUTTON.bluePzlistBtn);
                }

            }else{
                showBtn = [LIST_BUTTON.unCommit, LIST_BUTTON.approvalLink];
            }
            
        }
        // 单选数据时，将按钮去除不可点击状态
        props.button.setButtonDisabled([...showBtn, LIST_BUTTON.print, LIST_BUTTON.output, LIST_BUTTON.attachment, LIST_BUTTON.linkGroup, LIST_BUTTON.billTrack ,LIST_BUTTON.approver], false);
    }

}

//全选
export function selectedAllEvent(props, moduleId, status, length) {
    // 调用默认禁用按钮方法 true未不可点击态，false未可以点击态
    props.button.setButtonDisabled(LIST_DISABLED_BUTTON, true);

    let selectDatas = props.table.getCheckedRows(LIST.table_id);
    //判断选择表格中的的数量来进行选择
    if (selectDatas.length > 0) {
        let showBtn = [], deleteFlag = true, isAllNoState = true, isAllPassing = true ,isAllapprover = true;
        // 表格区
        let selectDatas = props.table.getCheckedRows(LIST.table_id);
        showBtn = [LIST_BUTTON.print, LIST_BUTTON.output];
        for (let selectData of selectDatas) {
            let status = selectData.data.values[FIELD.billStatus].value;
            // 通过态、进行态、提交态
            if (status == STATUS.PASSING || status == STATUS.COMMIT || status == STATUS.GOINGON) {
                isAllNoState = false;
                deleteFlag = false;
                isAllapprover = false;
                // 未通过态
            } else if (status == STATUS.NOSTATE) {
                isAllPassing = false;
            }
        }
        if (deleteFlag) {
            showBtn.push(LIST_BUTTON.delete);
        }
        if (isAllNoState) {
            showBtn.push(LIST_BUTTON.commit);
        }
        if (isAllPassing) {
            showBtn.push(LIST_BUTTON.unCommit);
        }
        if(isAllapprover) {
            showBtn.push(LIST_BUTTON.approver);
        }
        props.button.setButtonDisabled(showBtn, false);
    }
}
